package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PrivilegeGasConfDao;
import com.sc.nft.dao.PrivilegeRuleDao;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.PrivilegeGasConf;
import com.sc.nft.entity.PrivilegeRule;
import com.sc.nft.entity.req.EcologyCastGasRequest;
import com.sc.nft.entity.vo.ProductRuleConsensusVO;
import com.sc.nft.entity.vo.ProductRuleVO;
import com.sc.nft.entity.vo.ecology.EcologyCastGasVO;
import com.sc.nft.enums.GasConsumeTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PrivilegeRuleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author rdg
 * @since 2022-06-28 16:01:14
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PrivilegeRuleServiceImpl implements PrivilegeRuleService {
    private final PrivilegeRuleDao privilegeRuleDao;
    private final PrivilegeGasConfDao privilegeGasConfDao;

    @Override
    public Page<ProductRuleVO> getProductRule(int pageNo, int pageSize, Long productId) {
        return privilegeRuleDao.getProductRule(pageNo, pageSize, productId);
    }

    /**
     * 查询共识度是否消耗
     *
     * @param productId
     * @return
     */
    @Override
    public ProductRuleConsensusVO getCollectionRuleConsensus(Long productId) {
        ProductRuleConsensusVO collectionRuleConsensus = privilegeRuleDao.getCollectionRuleConsensus(productId);
        PrivilegeGasConf privilegeGasConf = privilegeGasConfDao.getPrivilegeGasConfByProductId(productId);
        if(Objects.nonNull(privilegeGasConf)){
            EcologyCastGasVO ecologyCastGasVO=new EcologyCastGasVO();
            ecologyCastGasVO.setGasConf(privilegeGasConf.getGasConf());
            ecologyCastGasVO.setConsumeCost(privilegeGasConf.getConsumeCost());
            ecologyCastGasVO.setConsumeType(privilegeGasConf.getConsumeType());
            collectionRuleConsensus.setEcologyCastGas(ecologyCastGasVO);
        }
        return collectionRuleConsensus;
    }

    @Override
    public List<PrivilegeRule> getByProductIdAndConsumeId(Long productId, Long consumeCollectionId) {
        return privilegeRuleDao.getByProductIdAndConsumeId(productId, consumeCollectionId);
    }

    /**
     * @param productId
     * @param consumeConsensus
     */
    @Override
    public void saveCollectionRuleConsensus(Long productId, Boolean consumeConsensus, EcologyCastGasRequest ecologyCastGasRequest) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(productId);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException("未找到该商品请稍后重试");
        }
        if (digitalCollectionProductInfo.getStatus()) {
            throw new GlobalRunTimeException("商品上架中不能做设置");
        }
        privilegeRuleDao.saveCollectionRuleConsensus(productId, consumeConsensus);

        if (Objects.nonNull(ecologyCastGasRequest)) {
            PrivilegeGasConf gasConf = privilegeGasConfDao.getPrivilegeGasConfByProductId(productId);
            if (Objects.isNull(gasConf)) {
                gasConf = new PrivilegeGasConf();
                gasConf.setGasConf(ecologyCastGasRequest.getGasConf());
                gasConf.setProductId(productId);
                gasConf.setCollectionId(digitalCollectionProductInfo.getRelationId());
                gasConf.setConsumeType(GasConsumeTypeEnum.BALANCE);
                gasConf.setConsumeCost(ecologyCastGasRequest.getConsumeCost());
                if (!privilegeGasConfDao.save(gasConf)) {
                    throw new GlobalRunTimeException("合成上链费保存失败！");
                }
            } else {
                gasConf.setConsumeCost(ecologyCastGasRequest.getConsumeCost());
                gasConf.setGasConf(ecologyCastGasRequest.getGasConf());
                if (!privilegeGasConfDao.updateById(gasConf)) {
                    throw new GlobalRunTimeException("合成上链费更新失败！");
                }
            }
        }
    }
}
